/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import util.DialogkeyListener;
import controller.ControllerInterface;
import controller.ImageViewerController;
import util.ImageDb;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JDialog;

/**
 *
 * @author Daniel
 */
public class SliderDialog extends JDialog implements WindowListener {
    private PanelSlider slider;    
    private DialogkeyListener listener;
    private ArrayList<ImageDb> slides;
    private int pos = 0;
    private int max = 0;
    private ControllerInterface controller;

    public SliderDialog(Frame owner, ControllerInterface controller) {
        super(owner, true);
        setResizable(true);
        this.setTitle("Slide View");
        this.controller = controller;
        
        initComponents();        
    }
    
    public void initComponents()
    {
        int w  = getToolkit().getScreenSize().width - 30;
        int h  = getToolkit().getScreenSize().height - 30;
        setSize(w, h);
        Container contentPane = getContentPane();
        slider = new PanelSlider();        

        listener = new DialogkeyListener(this);
        slider.setNameKeyListener(listener);
        slider.setCatKeyListener(listener);
        slider.setConfKeyListener(listener);
        slider.setSizeKeyListener(listener);
        addWindowListener(this);
        
        slider.setNextActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {

                    if(slides != null && pos+1 < max){
                        pos++;
                        setImagePath(slides.get(pos).getPath());
                        ImageDb t = slides.get(pos);
                        slider.setTextCat(t.getTopic());
                        if(t.getConfidentiality() == true)
                            slider.setTextConf("true");
                        else
                            slider.setTextConf("false");
                        slider.setTextName(t.getName());
                        slider.setTextSize(t.getSize());
                        slider.setDesText(t.getDescription());
                    }
                    
                }
            });
            
        slider.setBackActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {

                    if(slides != null && pos > 0){
                        pos--;
                        setImagePath(slides.get(pos).getPath());
                        ImageDb t = slides.get(pos);
                        slider.setTextCat(t.getTopic());
                        if(t.getConfidentiality() == true)
                            slider.setTextConf("true");
                        else
                            slider.setTextConf("false");
                        slider.setTextName(t.getName());
                        slider.setTextSize(t.getSize());
                        slider.setDesText(t.getDescription());
                    }                               
                }
            });
        
         slider.addModifyActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    ImageDb imagen = new ImageDb();
                    imagen.setPath(slides.get(pos).getPath());
                    imagen.setName(slider.getTextName());
                    imagen.setConfidentiality(slider.getTextConf());
                    imagen.setDescription(slider.getDescription());
                    imagen.setTopic(slider.getTextCat());
                    imagen.setExtension(slides.get(pos).getExtension());
                    imagen.setSize(slider.getTextSize());
                    controller.alterPictureTable(imagen);
                    slides.set(pos, imagen);
                }                                               
            });
            
         slider.addFavActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    controller.insertToFavorites(slides.get(pos).getPath());
                                                
                }                                               
            });            
            
        contentPane.add(slider);
       
    }   
    
    //cargalas imagenes a mostrar y la posicion del array que se muestra por default
    public void setImages(ArrayList<ImageDb> img, int posVis, int max)
    {
        //System.out.println("images: " + img + " posVis: " + posVis);
        if(img == null)
            return;

        slides = img;
        pos = posVis;
        this.max = max;
        setImagePath(slides.get(posVis).getPath());
        ImageDb t = slides.get(posVis);
        slider.setTextCat(t.getTopic());
        if(t.getConfidentiality() == true)
            slider.setTextConf("true");
        else
            slider.setTextConf("false");
        slider.setTextName(t.getName());
        slider.setTextSize(t.getSize());
        slider.setDesText(t.getDescription());        
    }
    
    public void setTextName(String nn)
    {
        slider.setTextName(nn);
    }
    
    public void setTextCat(String nn)
    {
        slider.setTextCat(nn);
    }
        
    public void setTextConf(String nn)
    {
        slider.setTextConf(nn);
    }
            
    public void setTextSize(String nn)
    {
        slider.setTextSize(nn);
    }
    
    public void setImagePath(String path)
    {
    	try {
            Image temp = ImageIO.read(new File(path));
            slider.setImage(temp);
        } catch(IOException e) {            
            e.printStackTrace();
	}       
    }
    
    public boolean showDialog()
    {                    
        setVisible(true); //este llamado no regresa hasta que el JDialog se borra        
        return true;
    }

    public void windowClosed(WindowEvent e) {
        setVisible(false);
    }

    public void windowOpened(WindowEvent e) {  }
    public void windowClosing(WindowEvent e) { }
    public void windowIconified(WindowEvent e) { }
    public void windowDeiconified(WindowEvent e) { }
    public void windowActivated(WindowEvent e) { }
    public void windowDeactivated(WindowEvent e) { }    
}
